package cn.exrick.manager.task;

import cn.exrick.common.constant.CommonConstant;
import cn.exrick.manager.pojo.TbEmailTemplate;
import cn.exrick.manager.pojo.TbItem;
import cn.exrick.manager.pojo.TbOrganization;
import cn.exrick.manager.pojo.TbUser;
import cn.exrick.manager.service.EmailTemplateService;
import cn.exrick.manager.service.ItemService;
import cn.exrick.manager.service.OrganizationService;
import cn.exrick.manager.service.UserService;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class EmailRemindJob {

    private static final Logger log= LoggerFactory.getLogger(EmailRemindJob.class);
    @Autowired
    private EmailTemplateService emailTemplateService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private UserService userService;
    @Autowired
    private ItemService itemService;

    @Scheduled(cron = "0 0 9 * * ?")
    public void run(){
        log.info("开始执行部门余额不足和库存不足通知的定时任务 - " + DateUtil.now());
        List<TbUser> userList = userService.getAllAdmin();
        List<TbOrganization> orgList = organizationService.getBalanceNotEnoughOrgList();
        if(CollectionUtils.isNotEmpty(orgList)){
            TbEmailTemplate EmailTemplate = emailTemplateService.getTemplateByType(CommonConstant.EMAIL_REMIND_TYPE_ORG_BALANCE);
            String template = EmailTemplate.getTemplate();
            if(template.contains("${部门名称}")){
                String orgName = "";
                for(TbOrganization organization : orgList){
                    orgName += organization.getName() + "，";
                }
                orgName = orgName.substring(0,orgName.length()-1);
                log.info("余额不足的部门："+ orgName);
                template =  template.replace("${部门名称}",orgName);
            }

            for(TbUser user : userList){
                String content = template;
                if(content.contains("${收件人名称}")){
                    content = content.replace("${收件人名称}",user.getUsername());
                }

                if(StringUtils.isNotBlank(user.getEmail())){
                    emailTemplateService.sendEmail("纪念品商城部门余额不足通知",user.getEmail(),content);
                }
            }
        }

        List<TbItem> itemList = itemService.getStockNotEnoughItemList();
        if(CollectionUtils.isNotEmpty(itemList)){
            TbEmailTemplate EmailTemplate = emailTemplateService.getTemplateByType(CommonConstant.EMAIL_REMIND_TYPE_STOCK);
            String template = EmailTemplate.getTemplate();
            if(template.contains("${商品名称}")){
                String itemTitle = "";
                for(TbItem tbItem : itemList){
                    itemTitle += tbItem.getTitle() + "，";
                }
                itemTitle = itemTitle.substring(0,itemTitle.length()-1);
                log.info("库存不足的商品："+ itemTitle);
                template =  template.replace("${商品名称}",itemTitle);
            }

            for(TbUser user : userList){
                String content = template;
                if(content.contains("${收件人名称}")){
                    content = content.replace("${收件人名称}",user.getUsername());
                }

                if(StringUtils.isNotBlank(user.getEmail())){
                    emailTemplateService.sendEmail("纪念品商城商品库存不足通知",user.getEmail(),content);
                }
            }
        }

        log.info("部门余额不足和库存不足通知的定时任务执行完毕 - " + DateUtil.now());
    }

}
